//
//  UITableView+Activity.m
//  TengyunStation
//
//  Created by Johnny on 15/9/11.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "UITableView+Activity.h"
#import "TSHeader.h"

#define TipViewHeight       200
#define TipViewTag          389

@implementation UITableView (Activity)

- (void)makeNone {
    self.backgroundView = nil;
}
- (CABasicAnimation *)addAnimation {
    CABasicAnimation *rotate =  [CABasicAnimation animationWithKeyPath: @"transform.rotation.z"];
    rotate.removedOnCompletion = FALSE;
    rotate.fillMode = kCAFillModeForwards;
    [rotate setToValue: [NSNumber numberWithFloat: M_PI * 2]];
    rotate.repeatCount = HUGE_VALF;
    rotate.duration = 1.5;
    rotate.cumulative = TRUE;
    rotate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    return rotate;
}
//加载中
- (void)makeLoading {
    UIView *tipView = [self loadTipView];
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activity.frame = CGRectMake(100, (TipViewHeight - 32)/2.0, 32, 32);
    [activity startAnimating];
    [tipView addSubview:activity];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(135, (TipViewHeight - 20)/2.0, 100, 20)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.textColor = TipTextColor;
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.text = @"正在查询中...";
    [tipView addSubview:tipLabel];
    self.backgroundView = tipView;
}

//加载无结果
- (void)makeTip:(NSString *)text {
    UIView *tipView = [self loadTipView];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(123, (TipViewHeight - 74 - 20 - 15)/2.0, 74, 74)];
    imageView.image = [UIImage imageNamed:@"yitui_icon_wjl"];
    [tipView addSubview:imageView];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, imageView.frame.origin.y + imageView.frame.size.height + 15, 296, 20)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.textColor = TipTextColor;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.text = text;
    [tipView addSubview:tipLabel];
    self.backgroundView = tipView;
}

//加载失败需要按钮重新触发
- (void)makeTip:(NSString *)text Title:(NSString *)title Target:(id)target Selector:(SEL)selector {
    UIView *tipView = [self loadTipView];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(123, (TipViewHeight - 74 - 20 - 15 - 10 - 32)/2.0, 74, 74)];
    imageView.image = [UIImage imageNamed:@"tuij_icon_wudh"];
    [tipView addSubview:imageView];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, imageView.frame.origin.y + imageView.frame.size.height + 15, 296, 20)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.textColor = TipTextColor;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.text = text;
    [tipView addSubview:tipLabel];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(105.5, tipLabel.frame.origin.y + tipLabel.frame.size.height + 10, 109, 32);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:OrangeColor forState:UIControlStateNormal];
    [button setTitleColor:TipTextColor forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageNamed:@"btn_chongxsm"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"btn_chongxsm_pre"] forState:UIControlStateHighlighted];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [tipView addSubview:button];
    self.backgroundView = tipView;
}

- (void)setBackgroundView:(UIView *)backgroundView {
    if (backgroundView) {
        UIView *view = [self viewWithTag:TipViewTag];
        if (view) {
            [view removeFromSuperview];
        }
        [self addSubview:backgroundView];
    } else {
        UIView *view = [self viewWithTag:TipViewTag];
        if (view) {
            [view removeFromSuperview];
        }
    }
}

- (UIView *)loadTipView {
    UIView *tipView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, TipViewHeight)];
    tipView.backgroundColor = [UIColor clearColor];
    tipView.userInteractionEnabled = YES;
    tipView.tag = TipViewTag;
    return tipView;
}

- (void)makeNoAuthodExpresserTip {
    UIView *tipView = [self loadTipView];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(123, 30, 74, 74)];
    imageView.image = [UIImage imageNamed:@"noauthodexpresser"];
    [tipView addSubview:imageView];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, imageView.frame.origin.y + imageView.frame.size.height + 15, 296, 15)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.textColor = AssistTextColor;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:15];
    tipLabel.text = @"无已授权的快递员";
    [tipView addSubview:tipLabel];
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, tipLabel.frame.origin.y + tipLabel.frame.size.height + 10, 280, 35)];
    detailLabel.backgroundColor = [UIColor clearColor];
    detailLabel.textColor = TipTextColor;
    detailLabel.font = [UIFont systemFontOfSize:13];
    detailLabel.textAlignment = NSTextAlignmentCenter;
    detailLabel.numberOfLines = 2;
    detailLabel.text = @"仅当快递员在管家APP中对您的腾云小站进行授权，您才有权限接收来自该快递员的快件。";
    [tipView addSubview:detailLabel];
    self.backgroundView = tipView;
}

- (void)makeNoImageTip:(NSString *)text Title:(NSString *)title Target:(id)target Selector:(SEL)selector {
    UIView *tipView = [self loadTipView];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(123, (TipViewHeight - 74 - 20 - 15 - 10 - 32)/2.0, 74, 74)];
    //    imageView.image = [UIImage imageNamed:@"tuij_icon_wudh"];
    [tipView addSubview:imageView];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, imageView.frame.origin.y + imageView.frame.size.height + 15, 296, 20)];
    tipLabel.backgroundColor = [UIColor clearColor];
    tipLabel.textColor = TipTextColor;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.text = text;
    tipLabel.numberOfLines = 0;
    [tipLabel sizeToFit];
    [tipView addSubview:tipLabel];
    tipLabel.frame = CGRectMake((320 - tipLabel.frame.size.width)/2.0, tipLabel.frame.origin.y, tipLabel.frame.size.width, tipLabel.frame.size.height);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(80, tipLabel.frame.origin.y + tipLabel.frame.size.height + 10, 160, 38);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:OrangeColor forState:UIControlStateNormal];
    [button setTitleColor:TipTextColor forState:UIControlStateHighlighted];
    button.clipsToBounds = YES;
    button.layer.cornerRadius = 4;
    button.layer.borderColor = OrangeColor.CGColor;
    button.layer.borderWidth = 1;
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [tipView addSubview:button];
    self.backgroundView = tipView;
}

@end
